package com.qingfeng.exception;

import com.qingfeng.util.ApiMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一异常处理
 */
@ControllerAdvice
public class UnifiedExceptionHandler {
    private final Logger logger =  LoggerFactory.getLogger(Exception.class);
    public static String method;

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
//    @ResponseStatus(HttpStatus.HTTP_VERSION_NOT_SUPPORTED)
    public ApiMsg unifiedExceptionHandler(Exception e){
        logger.error("updatePeopleGd() end===>" + e);
        ApiMsg responseMessage = new ApiMsg(0,e.getMessage(),"");
        logger.error(method+e);  //这里必须是logger.error(str1,str2),需要在
        e.printStackTrace();
        return responseMessage;
    }

    @ResponseBody
    @ExceptionHandler(value = LsException.class)
    public ApiMsg myExceptionHandler(LsException e){
        Map<String,Object> map  = new HashMap<String,Object>();
        ApiMsg responseMessage = new ApiMsg(0,e.getMessage(),"");
        return responseMessage;
    }



}
